//
//  Detail_MapSearchVC.m
//  HappyRent
//
//  Created by CoderYee on 2017/4/6.
//  Copyright © 2017年 内圈科技. All rights reserved.
//

#import "Detail_MapSearchVC.h"

@interface Detail_MapSearchVC ()<UISearchBarDelegate,BMKPoiSearchDelegate>
{
    UISearchBar      *_searchBar;
    BMKPoiSearch     *_poisearch;
    
    NSMutableArray   *_listArray;
}
@end

@implementation Detail_MapSearchVC

-(void)viewWillAppear:(BOOL)animated {
   
    _poisearch.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
}

-(void)viewWillDisappear:(BOOL)animated {
    
    _poisearch.delegate = nil; // 不用时，置nil
}
-(void)configOwnViews{
    
    [super configOwnViews];
    self.tableView.frame=CGRectMake(0, KNavigationBarHeight+KStatusBarHeight, kMainScreenWidth, KcontentViewHeight);
    _listArray=[[NSMutableArray alloc] init];
}
-(void)addOwnViews{
    
    [super addOwnViews];
    [self  addsearchBar];
    [self  configPoiSearch];
}

#pragma mark 配置poi城市检索
-(void)configPoiSearch{
    
    _poisearch = [[BMKPoiSearch alloc]init];
}
-(void)addsearchBar{
   
    _searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, kMainScreenWidth, KNavigationBarHeight)];
    _searchBar.showsCancelButton=YES;
    _searchBar.delegate=self;
    [_searchBar becomeFirstResponder];
    [self.view addSubview:_searchBar];

}
#pragma mark  UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BaseTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:[BaseTableViewCell cellIdentifier] forIndexPath:indexPath];
    cell.textLabel.textColor=kBlackColor;
    
    BMKPoiInfo* poi =_listArray[indexPath.row];
    cell.textLabel.text=poi.address;
    return cell;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

#pragma mark UISearchBar
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    
    [self.view endEditing:YES];
}
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if ([text isEqualToString:@"\n"]) {
       
        [self.view endEditing:YES];
        [self beginPoiSerach:searchBar.text];
        return NO;
    }
    return YES;
}
-(void)beginPoiSerach:(NSString *)text{
    //城市检索
    //可考虑换成区域检索
    BMKCitySearchOption *citySearchOption = [[BMKCitySearchOption alloc]init];
    citySearchOption.pageIndex = 0;
    citySearchOption.pageCapacity = 35;
    citySearchOption.city= _currentCity;
    citySearchOption.keyword = text;
    BOOL flag = [_poisearch poiSearchInCity:citySearchOption];
    if(flag)
    {
      NSLog(@"城市内检索发送成功");
    }
    else
    {
      NSLog(@"城市内检索发送失败");
    }
}
#pragma mark implement BMKSearchDelegate
- (void)onGetPoiResult:(BMKPoiSearch *)searcher result:(BMKPoiResult*)result errorCode:(BMKSearchErrorCode)error
{

    if (error == BMK_SEARCH_NO_ERROR) {
        [_listArray removeAllObjects];
        for (int i = 0; i < result.poiInfoList.count; i++) {
            
            BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
            [_listArray addObject:poi];
        }
        [self.tableView reloadData];
      
    } else if (error == BMK_SEARCH_AMBIGUOUS_ROURE_ADDR){
        
        NSLog(@"起始点有歧义");
    } else {
        
        // 各种情况的判断。。。
    }
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}
@end
